//
//  DateTools.m
//  HEClander
//
//  Created by User on 16/5/11.
//  Copyright © 2016年 User. All rights reserved.
//

#import "DateTools.h"

NSCalendar * calendar;
@implementation DateTools
//获取当月的天数
+ (NSInteger)getNumberOfDaysInMonthbydate:(NSDate *)date
{
    calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法
    
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate:date];
    return range.length;
}

//获取指定日期的年，月，日，星期，时,分,秒信息
+ (NSDateComponents *) getDateInfobydate:(NSDate *)date
{
    calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
    // NSDateComponent 可以获得日期的详细信息，即日期的组
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSDateComponents *comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSWeekCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:date];
    
    return comps;
}

//获取指定日期的星期 NSDateComponents获取的星期数值会有偏差 需要计算
+ (int)weekdayFromDate:(NSDate*)inputDate {
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    //NSLog(@"Tool  ---------------   %ld",theComponents.weekday);
    int a = (int)theComponents.weekday;
    if (a == 1) {
        a = 7;
    }else{
        a = a - 1;
    }
    return a;
}

//字符串转化成日期
+ (NSDate *) strToDate:(NSString *)Str
{
//    NSString *dateStr = [NSString stringWithFormat:@"%@",Str];
//    NSLog(@"date= %@", Str);
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd"];
    NSDate *fromdate=[format dateFromString:Str];
//    NSLog(@"fromdate=%@  ",fromdate);
//    NSTimeZone *fromzone = [NSTimeZone localTimeZone];
//    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
//    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
   // NSLog(@"fromdate=%@ ------- fromDate=%@",fromdate,fromDate);
    
    return fromdate;
}

//计算该每月一号为 星期几
+ (int)setdate:(NSDate *)date
{
    NSDateComponents *comp = [DateTools getDateInfobydate:date];
    
    //获取 传入年份 月份 1号 的字符串
    NSString *weekstring = [NSString stringWithFormat:@"%d-%d-01",(int)comp.year,(int)comp.month];
    
    //转换成 日期
    //NSString *weekstring = @"2016-06-01";
    NSDate *Date = [DateTools strToDate:weekstring];
    
    //计算该每月一号为 星期几
    int Weekcomp = [DateTools weekdayFromDate:Date];
    
    //NSLog(@"lklkalkmalkdjaklsjdklasjdklas   %@  %d",Date,Weekcomp);
    return Weekcomp;
    
}

//获取标题上的 年和月
+ (NSMutableArray *)getYearAndMonth:(NSDate *)date
{
    NSDateComponents *comp = [DateTools getDateInfobydate:date];
    
    if (comp.month < 10) {
        comp.month = [[NSString stringWithFormat:@"0%d",(int)comp.month] intValue];
    }
    //获取 传入年份 月份 1号 的字符串
    NSString *string = [NSString stringWithFormat:@"%d年%d月",(int)comp.year,(int)comp.month];
    NSString *string1 = [NSString stringWithFormat:@"%d-%d",(int)comp.year,(int)comp.month];
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:string,string1, nil];
    return arr;
}

//计算日历的 数据
+ (NSMutableArray *)getDatesourcebyDate:(NSDate *)date
{
    NSMutableArray *Arr = [[NSMutableArray alloc] init];
    int one = [DateTools setdate:date];
    int num = (int)[DateTools getNumberOfDaysInMonthbydate:date];
    
    int dateYear = (int)[DateTools getDateInfobydate:date].year;
    int dateMonth = (int)[DateTools getDateInfobydate:date].month;
    
    NSDate *Today = [NSDate date];
    int todayYear = (int)[DateTools getDateInfobydate:Today].year;
    int todayMonth = (int)[DateTools getDateInfobydate:Today].month;
    int todayDay = (int)[DateTools getDateInfobydate:Today].day;
    //NSLog(@"");
    
    if (one == 7) {
        one = 0;
    }
    //NSLog(@"one------------------------    %d",one);
    for (int i = 1; i <43; i ++) {
        if (i < one || i > one + num ) {
            [Arr addObject:@""];
        }
        else{
           NSString *str = [NSString stringWithFormat:@"%d",i-one];
            if ([str intValue] == 0) {
                str = @"";
            }
            if (dateYear == todayYear && dateMonth == todayMonth) {
                if ([str intValue] == todayDay) {
                    str = @"今";
                }
            }
        [Arr addObject:str];
        }
    }
    return Arr;
}

//上个月 下个月
+ (NSDate *)changeMonth:(NSDate *)date add:(int)MonthValue
{
    NSDateComponents *comps = [DateTools getDateInfobydate:date];
    NSDateComponents *comps1 = [DateTools getDateInfobydate:[NSDate date]];
    
    
    int year = (int)comps.year;
    
    
    int month = (int)comps.month;
    int year1 = (int)comps1.year;
    
    
    int month1 = (int)comps1.month;
    
    
    
    NSString *monthAdd = [NSString stringWithFormat:@"%d",month + MonthValue];
    NSString  *day = [NSString stringWithFormat:@"%ld",comps.day];
    
    
    
    
    //判断月份为12时 加1 则 年份加1 月份变为1
    //月份为1时 减1 则 年份减1 月份变为12
    if ([monthAdd intValue] == 0) {
        year = year - 1;
        monthAdd = @"12";
    }else if ([monthAdd intValue] == 13){
        year = year + 1;
        monthAdd = @"1";
    }
    NSDate *add = [DateTools strToDate:[NSString stringWithFormat:@"%d-%@-01",year,monthAdd]];
    int num = (int)[DateTools getNumberOfDaysInMonthbydate:add];
    
    
    if ([monthAdd intValue] < 10) {
        monthAdd = [NSString stringWithFormat:@"0%@",monthAdd];
    }
    if ([day intValue] < 10) {
        day = [NSString stringWithFormat:@"0%@",day];
    }
    
    //    NSLog(@"9999999------------------  %@",monthAdd);
    
    NSString *str = [NSString string];
    if (year == year1 && [monthAdd intValue] ==month1) {
        day = [NSString stringWithFormat:@"%ld",comps1.day];
        
    }else{
        day = [NSString stringWithFormat:@"%d",num];
//        NSLog(@"0------------------  %d ",(int)comps1.day);
    }
    
    str = [NSString stringWithFormat:@"%d-%@-%@",year,monthAdd,day];
    
    //    NSLog(@"0------------------  %d %d  %@",year%4,month,str);
    NSDate *Date = [DateTools strToDate:str];
    
//        NSLog(@"0------------------  %@",str);
    return Date;
}

@end
